<?php
/**
 * Created by PhpStorm.
 * User: sxg
 * Date: 2023-07-15
 * Time: 10:26
 */
require_once "vendor/autoload.php";

ini_set("memory_limit", "2048M");

use Socket\Ms\Protocols\Mqtt;
use Socket\Ms\Response;

class tcp {

    private $_server;

    public function __construct() {
        $this->_server = new Socket\Ms\Server("tcp://0.0.0.0:9501");
        $this->_server->settings([
            'workerNum' => 1,
            'unix_server_socket_file' => '/home/sxg/te/sock/unix_sock_server.sock',
            'unix_client_socket_file' => '/home/sxg/te/sock/unix_sock_client.sock',
            'daemon' => false
        ]);
        $this->_server->on("connect", [$this, "onConnect"]);
        $this->_server->on("receive", [$this, "onReceive"]);
        $this->_server->on("close", [$this, "onClose"]);
        $this->_server->start();
    }

    public function onConnect(Socket\Ms\Server $server, Socket\Ms\TcpConnections $TcpConnections) {
        $server->echoLog("有客户端连接了");
    }

    public function onReceive(Socket\Ms\Server $server, $msg, Socket\Ms\TcpConnections $connection) {
        $server->echoLog("有客户发送数据:%s", $msg);
        $connection->send("sxg");
    }


    public function onClose(Socket\Ms\Server $server, Socket\Ms\TcpConnections $connection) {
        $server->removeClient($connection->_socketFd);
    }

}

$ms = new tcp();
